<?php
session_start(); 

include "webmaster_connect.php";

$product_type 			= $_POST['product_type'];
$product_catagory 		= $_POST['product_catagory'];
$product_sub_catagory	= $_POST['product_sub_catagory'];
$product_name 			= $_POST['Product_name'];
$product_status 		= $_POST['Product_status'];
$product_cost 			= $_POST['Product_cost'];
$product_description	= $_POST['product_description'];
$product_make 			= $_POST['Product_make'];
$product_colour 		= $_POST['Product_colour'];
$product_size 			= $_POST['Product_size'];
$product_offer 			= $_POST['Product_offer'];

$data['page_name'] = $product_name;
$data['page_name'] = preg_replace('/\s+/', '_', $data['page_name']);
$link = $data['page_name'];

//Create product ID

$get_words = $product_type."_".$product_catagory."_".$product_sub_catagory;

$words = explode("_", $get_words);
$acronym = "";

foreach ($words as $w) {
  $acronym .= $w[0];
}

$acronym = strtoupper($acronym); // Convert to uppercase

$product_id = $acronym;

mysql_query("INSERT INTO saddles

(

product_type,
product_id,
product_catagory, 		
product_sub_catagory,	
product_name, 			
product_status, 		
product_cost, 			
product_description,	
product_make, 			
product_colour, 		
product_size, 			
product_offer,
link

) 

VALUES

(

'$product_type',
'$product_id',
'$product_catagory',	
'$product_sub_catagory',	
'$product_name',		
'$product_status',		
'$product_cost', 		
'$product_description',
'$product_make ',			
'$product_colour ',		
'$product_size',			
'$product_offer',
'$link'		
) 

"); 

$new_product_id = mysql_insert_id();

// Add row id to productID
$acronym = $acronym.$new_product_id;

mysql_query("UPDATE saddles SET 

product_id = '$acronym'

WHERE id = $new_product_id");

$_SESSION['new_product_id'] = $new_product_id;

//ERROR TESTING

//echo $product_type."<br />";		
//echo $product_catagory."<br />"; 		
//echo $product_sub_catagory."<br />";	
//echo $product_name."<br />"; 			
//echo $product_status."<br />"; 		
//echo $product_cost."<br />"; 			
//echo $product_description."<br />";	
//echo $product_make."<br />"; 			
//echo $product_colour."<br />"; 		
//echo $product_size."<br />"; 			
//echo $product_offer."<br />";

//echo $new_product_id."<br />";


//echo $acronym."<br />";




//////////////////////////////////Create product .php page/////////////////////////////////////////////




//name of the template file
$tpl_file = "template.php";

//path to the directory at the server, where you store the "template.html" file.
$tpl_path = "/home/z247s504/public_html/saddles/";

//path to the directory where you will store the auto-generated members pages.
$product_path = "/home/z247s504/public_html/saddles/"; 

$data['product_id'] = $new_product_id;

$placeholders = array("{product_id}"); 

$tpl = file_get_contents($tpl_path.$tpl_file); 

//Set PRODUCT ID
$new_product_file = str_replace("{product_id}", $data['product_id'], $tpl);
//Set PAGE TITLE 
$new_product_file = str_replace("{page_title}", $product_name, $new_product_file);
//Set PRODUCT NAME 
$new_product_file = str_replace("{product_name}", $product_name, $new_product_file); 

$php_file_name = $data['page_name'].".php"; 

$fp = fopen($product_path.$php_file_name, "w");
fwrite($fp, $new_product_file);
fclose($fp);

$_SESSION['active_product_id'] = $new_product_id;

header("Location: webmaster_amend_products.php");

?>